package com.dayuan.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuan.bean.UserInfo;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.UserInfoService;
import com.dayuan.util.PatternUtils;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Resource(name = "userInfoService")
	private UserInfoService uis;
	
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);

	/**
	 * 验证帐号密码是否正确
	 * 
	 * @param session
	 * @param user
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/check.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo login(HttpSession session, String user, String password) {
		ResultVo resultVo = new ResultVo();

		UserInfo ui = new UserInfo();

		if (password == null||user == null ||"".equals(user)||  "".equals(password) ) {
			resultVo.setMsg("帐号密码不能为空");
			return resultVo;
		}
		if (PatternUtils.isEmail(user)) {

			ui.setEmail(user);
			ui.setPassword(password);
		} else if (PatternUtils.isMobile(user)) {

			ui.setMobile(user);
			ui.setPassword(password);
		} else {
			ui.setUsername(user);
			ui.setPassword(password);
		}
		logger.info("用户准备开始验证密码{}",ui.getId());
		logger.info("111111");
		UserInfo userinfo = uis.selectUser(ui);

		if (userinfo == null) {
			resultVo.setCode(AjaxMsg.LOGIN_FAIL.getValue());
			resultVo.setMsg("账号密码错误");
			return resultVo;
		} else if (!ui.getPassword().equals(userinfo.getPassword())) {
			resultVo.setCode(AjaxMsg.LOGIN_FAIL.getValue());
			resultVo.setMsg("账号密码错误");
			return resultVo;
		}

		session.setAttribute("userinfo", userinfo);
		resultVo.setCode(AjaxMsg.LOGIN_SUCCESS.getValue());

		resultVo.setMsg("登陆成功");
		logger.info("登录状态为{}",resultVo.getCode());
		return resultVo;
	}

	/**
	 * 返回用户session存储的用户信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/query.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo queryinfo(HttpSession session) {
		ResultVo resultVo = new ResultVo();

		try {

			UserInfo userInfo = session.getAttribute("userinfo") == null ? null
					: (UserInfo) session.getAttribute("userinfo");
			resultVo.setCode(AjaxMsg.LOGIN_SUCCESS.getValue());
			resultVo.setData(userInfo);
			return resultVo;
		} catch (Exception e) {

			resultVo.setCode(AjaxMsg.LOGIN_FAIL.getValue());
			resultVo.setMsg("网络不稳定");

			return resultVo;
		}

	}

	/**
	 * 退出登录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/clean.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo clean(HttpSession session) {
		ResultVo resultVo = new ResultVo();

		try {

			session.invalidate();
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setMsg("退出成功");
			return resultVo;
		} catch (Exception e) {

			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络不稳定");

			return resultVo;
		}

	}
}
